package com.xhh.nphm.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.system.domain.SensitiveInfo;
import com.xhh.nphm.system.domain.SensitiveInfoDetail;
import com.xhh.nphm.system.mapper.SensitiveInfoDao;
import com.xhh.nphm.system.service.ISensitiveInfoService;
import com.xhh.nphm.system.service.SensitiveInfoDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 敏感信息业务接口实现类
 * @author zz
 */
@Service
public class SensitiveInfoServiceImpl extends ServiceImpl<SensitiveInfoDao, SensitiveInfo> implements ISensitiveInfoService {
    /**
     * 敏感信息详情业务层对象
     */
    @Autowired
    private SensitiveInfoDetailService infoDetailService;

    /**
     * 自定义敏感信息添加业务实现
     * @param sensitiveInfo 敏感信息数据
     * @return 添加后生成的敏感信息主键Id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(SensitiveInfo sensitiveInfo) {
        //是否添加敏感信息成功
        boolean flag = getBaseMapper().insert(sensitiveInfo) > 0;
        //添加敏感信息详情
        for(SensitiveInfoDetail detail: sensitiveInfo.getDetails()){
            detail.setSensitiveId(sensitiveInfo.getId());
            infoDetailService.save(detail);
        }
        //成功返回生成的敏感信息主键Id 失败返回0
        return flag ? sensitiveInfo.getId() : 0;
    }

    /**
     * 通过pkid获取一条敏感信息
     *
     * @param pkId 来电记录id
     * @return 敏感信息
     */
    @Override
    public SensitiveInfo findOneByPkId(Integer pkId) {
        return getBaseMapper().findOneByPkId(pkId);
    }

    /**
     * 自定义敏感信息修改业务实现
     *
     * @param sensitiveInfo 敏感信息数据
     * @return 成功标识
     */
    @Override
    public boolean updateInfoDetail(SensitiveInfo sensitiveInfo) {
        //是否修改敏感信息成功
        boolean flag = false;
        //修改敏感信息详情
        for(SensitiveInfoDetail detail: sensitiveInfo.getDetails()){
            detail.setSensitiveId(sensitiveInfo.getId());
            flag = infoDetailService.saveOrUpdate(detail);
        }
        return flag;
    }

    /**
     * 通过id获取一条敏感信息
     *
     * @param id 敏感信息主键id
     * @return 敏感信息
     */
    @Override
    public SensitiveInfo findOneById(Integer id) {
        return getBaseMapper().findOneById(id);
    }
}
